import Mock, { Random } from 'mockjs'
import { saveData, setPageData, delData } from './common-mock-method'

const pageData = Mock.mock({
    'list|79': [
        {
            'id|+1': 1,
            createTime: Random.time(),
            updateTime: Random.time(),
            title: '@cword(2,7)',
            subTitle: '@cword(1,5)',
            content: '@cword(10,80)',
            remarks: '@cword(0,30)',
        }
    ]
})
module.exports = [
    {  // page接口
        url: '/mock/playground/page',
        method: 'post',
        response: (config) => {
            return setPageData(pageData, config, 'title', 'subTitle')
        }
    },
    {  // 根据id获取
        url: RegExp('/mock/playground/get/' + '.*'),
        method: 'post',
        response: (config) => {
            const index = config.url.replace('/mock/playground/get/', '')
            const result = pageData.list.filter(({id}) => id === +index)?.[0]
            if (result) {
                return {
                    code: 200,
                    data: result,
                    message: 'OK'
                }
            } else {
                return {
                    code: 500,
                    data: '未找到该数据',
                    message: 'OK'
                }
            }
        }
    },
    {   // 保存
        url: '/mock/playground/save',
        method: 'post',
        response: (config) => {
            return saveData(config, pageData)
        }
    },
    {   // 删除
        url: RegExp('/mock/playground/del/' + '.*'),
        method: 'post',
        response: (config) => {
            return delData(config, '/mock/playground/del/', pageData)
        }
    }
]
